if not exists (select * from sys.schemas where name = N'foo')
exec sp_executesql N'create schema [foo] authorization [dbo]';
go

if not exists (select * from sys.objects where object_id = object_id(N'[foo].[foo_types]') and type in (N'U'))
create table [foo].[foo_types](
    [id] int not null,
    [name] varchar(32) not null,
    constraint [PK__foo_types] primary key clustered (
        [id] asc
    ) with (
        pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on
    ) on [default]
) on [default]
go

if not exists (select * from sys.objects where object_id = object_id(N'[foo].[do_not_overwrite_this_type]') and type in (N'U'))
create table [foo].[do_not_overwrite_this_type](
    [id] int not null identity(1,1),
    [blargy_blarg] nvarchar(max) not null,
    constraint [PK__do_not_overwrite_this_type] primary key clustered (
        [id] asc
    ) with (
        pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on
    ) on [default]
) on [default]
go

if not exists (select * from sys.objects where object_id = object_id(N'[foo].[foo_bars]') and type in (N'U'))
create table [foo].[foo_bars](
    [id] int not null identity(1,1),
    [foo_type_id] int not null constraint [FK__foo_types_foo_type_id__foo_bars_id] foreign key ([foo_type_id]) references [foo].[foo_types] ([id]),
    [blah_blah] nvarchar(max) not null,
    constraint [PK__foo_bars] primary key clustered (
        [id] asc
    ) with (
        pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on
    ) on [default]
) on [default]
go

if not exists (select * from sys.objects where object_id = object_id(N'[foo].[foo_other_bars]') and type in (N'U'))
create table [foo].[foo_other_bars](
    [id] int not null identity(1,1),
    [foo_type_id] int not null constraint [FK__foo_types_foo_type_id__foo_other_bars_id] foreign key ([foo_type_id]) references [foo].[foo_types] ([id]),
    [blah_blah] nvarchar(max) not null,
    constraint [PK__foo_other_bars] primary key clustered (
        [id] asc
    ) with (
        pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on
    ) on [default]
) on [default]
go

if not exists (select * from sys.objects where object_id = object_id(N'[foo].[foo_maps]') and type in (N'U'))
create table [foo].[foo_maps](
    [id] int not null identity(1,1),
    [foo_type_id] int not null constraint [FK__foo_types_foo_type_id__foo_maps_id] foreign key ([foo_type_id]) references [foo].[foo_types] ([id]),
    [value] int not null,
    constraint [PK__foo_maps] primary key clustered (
        [id] asc
    ) with (
        pad_index  = off, statistics_norecompute  = off, ignore_dup_key = off, allow_row_locks  = on, allow_page_locks  = on
    ) on [default]
) on [default]
go
